package gen.tools;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Authentication extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public Authentication() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		final Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/text");
		
		final String sql = "SELECT empName FROM emps WHERE empId=? AND empPwd=?";
		final String empId = request.getParameter("empId");
		final String empPwd = request.getParameter("empPwd");
		
		String res = "NoNoNo";
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = wid.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, empId);
			ps.setString(2, empPwd);
			rs = ps.executeQuery();
			
			if ( rs.next() ) {
				res = rs.getString("empName");
			}
		} catch(Exception e) {
			res  = wid.errLog(e);
		} finally {
			try {
				if ( rs !=null ) {rs.close();}
				if ( ps !=null ) {ps.close();}
				if ( conn !=null ) {conn.close();}	
			} catch (Exception e) { e.printStackTrace(); }
		}
		
		response.getWriter().write(res);
	}

}
